package com.hui.auth.dao;

import com.hui.auth.dao.sql.CommonSQLProvider;
import com.hui.auth.entity.AuthMenu;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 权限
 * Created by LiaoYunHui on 2017/9/23/023.
 */
@Mapper
public interface AuthMenuDao {

    @InsertProvider(type = CommonSQLProvider.class, method = "insert")
    @Options(useGeneratedKeys = true)
    boolean insert(AuthMenu menu);

    @UpdateProvider(type = CommonSQLProvider.class, method = "update")
    boolean update(AuthMenu menu);

    /**
     * 绑定角色菜单（批量）
     */
    boolean insertRoleMenu(@Param("roleId") long roleId, @Param("menus") List<Integer> menus);

    List<AuthMenu> selectAll();

    @Delete("delete from auth_menu where id = #{id}")
    Integer delete(Integer id);

    /**
     * 根据角色rid删除角色菜单
     */
    @Delete("delete from auth_role_menu where rid = #{rid}")
    Integer deleteByRid(Long rid);

    /**
     * 根据角色rid查询某一级的菜单
     */
    @Select("select m.* from auth_role_menu rm left join auth_menu m on rm.mid = m.id where rm.rid in (#{roleId}) and m.pid = #{pid} order by m.sort")
    List<AuthMenu> selectByRoleId(@Param("roleId") String roleId, @Param("pid") Long pid);

    /**
     * 查询子菜单
     */
    @Select("select * from auth_menu where pid = #{pid}")
    List<AuthMenu> selectChildren(long pid);

    /**
     * 根据角色rid查询菜单
     */
    @Select("select m.id from auth_role_menu rm inner join auth_menu m on rm.mid = m.id where rm.rid = #{rid}")
    List<Integer> selectIdListByRoleId(Long rid);

    /**
     * 根据角色rid查询菜单
     */
    @Select("select m.* from auth_role_menu rm inner join auth_menu m on rm.mid = m.id where rm.rid = #{rid}")
    List<AuthMenu> selectListByRoleId(Long rid);

    /**
     * 根据角色rid查询菜单（批量）
     */
    List<AuthMenu> selectListByRoleIds(List<Long> roleIds);
}
